package cn.txy.system.controller;

import cn.txy.config.R;
import cn.txy.system.annotation.PreAuthorize;
import cn.txy.system.domain.Employee;
import cn.txy.system.query.EmployeeQuery;
import cn.txy.system.service.EmployeeService;
import cn.txy.system.vo.EmployeeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询员工",sn="employee:getOne",descs = "根据ID查询员工")
    public R findById(@PathVariable("id")Long id){
        return R.ok(employeeService.findById(id));
    }

    @GetMapping
    @PreAuthorize(name = "查询员工",sn="employee:getAll",descs = "查询员工")
    public R findAll(){
        return R.ok(employeeService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除员工",sn="employee:delOne",descs = "根据ID删除员工")
    public R deleteById(@PathVariable("id")Long id){
        employeeService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(name = "删除员工",sn="employee:delAll",descs = "删除员工")
    public R batchDelete(@RequestBody List<Long> ids){
        employeeService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation("添加和修改")
    @PostMapping
    @PreAuthorize(name = "添加和修改员工",sn="employee:saveOne",descs = "添加和修改员工")
    public R add(@RequestBody EmployeeVo employeeVo){
        if(employeeVo.getId() == null){
            employeeService.add(employeeVo);
        }else {
            employeeService.update(employeeVo);
        }
        return R.ok();
    }
    @PostMapping("/page")
    @PreAuthorize(name = "分页查询员工",sn="employee:page",descs = "分页查询员工")
    public R getPage(@RequestBody EmployeeQuery employeeQuery){
        PageHelper.startPage(employeeQuery.getPage(),employeeQuery.getSize());
        List<Employee> employees = employeeService.queryPage(employeeQuery);
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
//        pageInfo.setTotal(employeeService.queryPageCount(employeeQuery));
        return R.ok(pageInfo);
    }
}
